/**
 * 功能描述 行编辑
 */

define(function (require, exports, module){
	var EditGrid=require("editgrid");
	var store =require("store");
	var activeUser = store.get("user");
	var Dialog = require("dialog");
	
	var instance = {
			initComponent: function (params,widget){
				var optiongrid = new EditGrid({
					parentNode:".J-editgrid",
					url : "api/roompersonalInfo/query",
					params : function(){
						var pkCIImplement = widget.get("pkCIImplement");
						var obj = {
	                			"checkInImplement" : pkCIImplement,
	                			fetchProperties : "pkRoomPersonalInfo,signUpRegister.personalInfo.name,roomRegister.room.code," +
	                					"roomRegister.room.status.*,roomRegister.room.orgBuilding.name,signUpRegister.personalInfo.idNumber," +
	                					"signUpRegister.personalInfo.sex,signUpRegister.personalInfo.mobilePhone," +
	                					"signUpRegister.member,signUpRegister.travelContract.memberCertificates," +
	                					"roomRegister.electric,roomRegister.water,roomRegister.waterElectric,roomRegister.checkInPrice," +
	                					"roomRegister.breakfast,checkIn,dataSource,signUpRegister.travelContract.unit,signUpRegister.travelContract.price,"+
	                					"SignUpRegister.travelContract.balTime,roomRegister.room.roomType.*,roomRegister.*,checkInImplement.*"
	                	};
		    			return obj;
		    			$(".J-grid-columns-roomRegister-checkInPrice").eq(0).click()
					},
					autoRender:false,
					model:{
						id:"optiongrid",
						allowAdd:false,
						columns:[{
							name:"orgBuildingAndRoom",
							label:"房间",
							format: function(val,rowEle){
								if(rowEle.roomRegister.room){
									if(rowEle.roomRegister.room.orgBuilding){
										return rowEle.roomRegister.room.orgBuilding.name+":"+rowEle.roomRegister.room.code;
									}else {
										return rowEle.roomRegister.room.code;
									}
								}else{
									return "";
								}
							}
						},{
							name:"roomRegister.room.roomType.name",
							label:"房型"
						},{
							name:"signUpRegister.personalInfo.name",
							label:"姓名",
						},{
							name:"signUpRegister.personalInfo.idNumber",
							label:"身份证号",
						},{
							name:"signUpRegister.personalInfo.sex.value",
							label:"性别",
						},{
							name:"signUpRegister.personalInfo.mobilePhone",
							label:"手机号",
						},{
							name:"signUpRegister.member",
							label:"会员",
							format:function(value,row){
								if(row.signUpRegister.member){
									return "是";
								}else{
									return "否";
								}
							}
						},{
							name:"signUpRegister.travelContract.memberCertificates",
							label:"会员证号",
						},{
							name:"roomRegister.waterElectric",
							label:"含水电费",
							editor:{
								type:"select",
								options:[{
									key:"true",
									value:"是"
								},{
									key:"false",
									value:"否"
								}],
								defaultValue:"",
								onBeforeEdit:function(e){
									return;
								},
								onEdit:function(editor,rowIndex,rowData){
									editor.setValue(rowData.roomRegister.waterElectric.key);
								},
								onChange:function(plugin,index,rowData){
									rowData.roomRegister.waterElectric = plugin.getData(plugin.getValue());
									optiongrid.update(index,rowData);
								}
							},
							format:function(value,row){
								if(typeof value == "object"){
									return value.value;
								}else{
									if(value == true || value == "true"){
										return "是";
									}else{
										return "否";
									}
								}
							}
						},{
							name:"roomRegister.breakfast",
							label:"含早餐",
							editor:{
								type:"select",
								options:[{
									key:"true",
									value:"是"
								},{
									key:"false",
									value:"否"
								}],
								defaultValue:"",
								onEdit:function(editor,rowIndex,rowData){
								    editor.setValue(rowData.roomRegister.breakfast.key);
								},
								onChange:function(plugin,index,rowData){
									rowData.roomRegister.breakfast = plugin.getData(plugin.getValue());
									optiongrid.update(index,rowData);
								}
							},
							format:function(value,row){
								if(typeof value == "object"){
										return value.value;
								}else{
									if(value == true || value == "true"){
										return "是";
									}else{
										return "否";
									}
								}
							}
						},{
							name:"checkInImplement.vipPrice",
							label:"房价",
							format:function(val){
								return Number(val).toFixed(2);
							}
						},{
							name:"roomRegister.checkInPrice",
							label:"入住价",
							format:function(val,rowData){
								if(rowData.signUpRegister.travelContract!=null&&rowData.signUpRegister.travelContract.unit.key=="Day"){
									val = rowData.signUpRegister.travelContract.price/rowData.signUpRegister.travelContract.balTime;
								}
								return Number(val).toFixed(2);
							},
							editor : {
								type:"text",
								onChange : function(plugin,index,rowData){
									var income = plugin.getValue();
									var reg = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
									var re = new RegExp(reg);
									if(income.search(re) != -1){
										rowData.roomRegister.checkInPrice = Number(plugin.getValue()).toFixed(2);
										optiongrid.update(index, rowData);
									}else{
										Dialog.alert({
											title : "提示",
											content : "请输入正确的价格！",
										})
										rowData.roomRegister.checkInPrice = 0.00;
										optiongrid.update(index,rowData);
									}
								},
								onBeforeEdit:function(rowData){
									if((rowData.signUpRegister.travelContract!=null && rowData.signUpRegister.travelContract.unit.key=="Day")||widget.get("check")==true){
										return false;
									}else{
										return true;
									}
								},
							}
						},{
							name : "checkIn",
							label : "状态",
							format:function(value,row){
								if(row.checkIn==true){
									return "已登记";
								}else{
									return "待登记";
								}
							}
						},{
							name:"operate",
							label:"操作",
							format:"button",
							formatparams:[{
								id:"appointment",
								text: "登记",
								show:function(value,row){		
									if(widget.get("check")==true){
										return false;
									}
									if(row.checkIn==true){
										return false;
									}else{
										return true;
									}
								},
								handler:function(index,data,rowEle){
									widget.handler.optiongrid.appointment(widget,data);
								}
							},{
								id:"cancel",
								text: "取消",
								show:function(value,row){
									if(widget.get("check")==true){
										return false;
									}
									if(row.checkIn==true){
										return true;
									}else{
										return false;
									}
								},
								handler:function(index,data,rowEle){
									widget.handler.optiongrid.cancel(widget,data)
								}
							}]
						}]
					}
				});
				widget.set("optiongrid",optiongrid);
			}
	};
	module.exports = instance;
});
